/**
 * 
 */
package com.feib.stms.dao.jdbc;


import java.sql.Types;

import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.dao.support.DataAccessUtils;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
import org.springframework.jdbc.support.incrementer.OracleSequenceMaxValueIncrementer;
import org.springframework.stereotype.Repository;

import com.feib.stms.dao.ConfirmRuleDao;
import com.feib.stms.dao.jdbc.rowmapper.ConfirmRuleRowMappper;
import com.feib.stms.model.ConfirmRule;
import com.feib.stms.model.Group;

/**
 * @author Jeff Gu
 *
 */
@Repository("confirmRuleDao")
public class ConfirmRuleJdbcDao extends BaseJdbcDao implements ConfirmRuleDao {


	protected OracleSequenceMaxValueIncrementer sequence;

	protected SimpleJdbcInsert confirmRuleJdbcInsert;

	@Override
	protected void initDao() throws Exception {
		super.initDao();
		this.sequence = new  OracleSequenceMaxValueIncrementer(this.getDataSource(), "CONFIRM_RULE_PK_SEQ");
		this.confirmRuleJdbcInsert = new  SimpleJdbcInsert(this.getDataSource());
		this.confirmRuleJdbcInsert.withTableName("CONFIRM_RULE")
		.usingColumns("CONFIRM_RULE_PK", "GROUP_PK", "CONFIRM_COUNT", "CHECK_COUNT", "CONFIRM_TYPE"
				, "IS_ENABLED","DES_KEY"
				, "UPDATED_DATE", "UPDATED_BY", "CREATED_DATE", "CREATED_BY");
	}





	public int insertConfirmRule(ConfirmRule confirmRule) {
		MapSqlParameterSource parameters = new MapSqlParameterSource();

		Long confirmRulePk = new Long(sequence.nextLongValue());
		parameters.addValue("CONFIRM_RULE_PK", confirmRulePk, Types.NUMERIC);
		parameters.addValue("GROUP_PK",confirmRule.getGroup().getGroupPk(), Types.NUMERIC);
		parameters.addValue("CONFIRM_COUNT", confirmRule.getConfirmCount(), Types.NUMERIC);
		parameters.addValue("CHECK_COUNT", confirmRule.getCheckCount(), Types.NUMERIC);
		parameters.addValue("CONFIRM_TYPE",  confirmRule.getConfirmType(), Types.NUMERIC);
		parameters.addValue("IS_ENABLED",  new Integer(1), Types.NUMERIC);
		parameters.addValue("DES_KEY",  confirmRule.getDesKey(), Types.VARCHAR);	
		parameters.addValue("CREATED_DATE",  confirmRule.getCreatedDate(), Types.TIMESTAMP);
		parameters.addValue("CREATED_BY", confirmRule.getCreatedBy(), Types.VARCHAR);
		
		int updateCounts = confirmRuleJdbcInsert.execute(parameters);

		if (updateCounts > 0)
			confirmRule.setConfirmRulePk(confirmRulePk);


		return updateCounts;
	}


	public ConfirmRule getConfirmRuleEnabled(Group group) {
		StringBuffer sql = new StringBuffer();
		MapSqlParameterSource parameters = new MapSqlParameterSource();

		sql.append("SELECT C.*");
		sql.append(" FROM CONFIRM_RULE C");
		sql.append(" WHERE C.GROUP_PK = :GROUP_PK AND IS_ENABLED =1 ");		
		parameters.addValue("GROUP_PK", group.getGroupPk(), Types.NUMERIC);

		try{
			return DataAccessUtils.singleResult(this.getSimpleJdbcTemplate().query(sql.toString(), new ConfirmRuleRowMappper(), parameters));
		}catch (EmptyResultDataAccessException e) {
			return null;
		}
	}


	public int disableConfirmRule(ConfirmRule confirmRule) {
		StringBuffer sql = new StringBuffer();
		MapSqlParameterSource parameters = new MapSqlParameterSource();

		sql.append("UPDATE CONFIRM_RULE SET  ");
		sql.append(" IS_ENABLED = :IS_ENABLED ,UPDATED_DATE= :UPDATED_DATE ,UPDATED_BY= :UPDATED_BY ");
		sql.append(" WHERE GROUP_PK = :GROUP_PK");


		parameters.addValue("IS_ENABLED", 0, Types.NUMERIC);
		parameters.addValue("UPDATED_DATE",  confirmRule.getUpdatedDate(), Types.TIMESTAMP);
		parameters.addValue("UPDATED_BY",  confirmRule.getUpdatedBy(), Types.VARCHAR);
		parameters.addValue("GROUP_PK",  confirmRule.getGroup().getGroupPk(), Types.NUMERIC);


		return this.getSimpleJdbcTemplate().update(sql.toString(), parameters);

	}


}
